﻿/*
 * Created by SharpDevelop.
 * User: 김효순
 * Date: 2012-08-31
 * Time: 오후 12:17
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 사운드
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	/// 
	public partial class MainForm : Form
	{
		private const int SND_ASYNC = 0x1;         //  play asynchronously
		private const int SND_NODEFAULT = 0x2;        //  silence not default, if sound not found
		private const int SND_LOOP = 0x8;         //  loop the sound until next sndPlaySound

		
		[DllImport("winmm.dll", SetLastError = true)]
		private static extern uint waveOutGetNumDevs();
		
		[DllImport("winmm.dll")]
		private static extern uint sndPlaySound(string lpszSoundName, uint uFlags);
		
		[DllImport("winmm.dll", SetLastError = true)]
		private static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, uint hwndCallback);
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Bt1Click(object sender, EventArgs e)
		{
			uint wogn = waveOutGetNumDevs();
			tb1.Text = wogn.ToString();
		}
		
		void Bt2Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = "음악파일|*.wav|All Files(*.*)|*.*";
			
			try{
				if(ofd.ShowDialog() == DialogResult.OK){
					uint wFlags = SND_ASYNC | SND_NODEFAULT;
					if(cb1.Checked) wFlags |= SND_LOOP;
					sndPlaySound(ofd.FileName, wFlags);
				}
			} catch(System.Exception ex){
				MessageBox.Show(ex.Message);
			}
		}
		
		void Bt3Click(object sender, EventArgs e)
		{
			sndPlaySound((string) null, 0);
		}
		
		void Bt4Click(object sender, EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "미디파일|*.mid|All Files(*.*)|*.*";
		
			if(dlg.ShowDialog() == DialogResult.OK){
				mciSendString("close all", (string) null, 0, 0);
				uint mci = mciSendString("open sequencer!\"" + dlg.FileName + "\" alias midi", (string) null, 0, 0);
		
				if(mci != 0){
					tb1.Text = "Open Error";
					return;
				}
		
				mci = mciSendString("play midi", (string) null, 0, 0);
				if(mci != 0){
					tb1.Text = "Play Error";
					return;
				}
			}
		}
		
		void Bt5Click(object sender, EventArgs e)
		{
			mciSendString("stop midi", (string) null, 0, 0);
			mciSendString("close midi", (string) null, 0, 0);
		}
		
		void Bt6Click(object sender, EventArgs e)
		{
			int bitPerSample=0, samplesPerSec=0, channel=0;
	
			if(rb1.Checked){
				bitPerSample = 8;
			}else if(rb2.Checked){
				bitPerSample = 16;
			}
		
			if(rb3.Checked){
				samplesPerSec = 11025;
			}else if(rb4.Checked ){
				samplesPerSec = 22050;
			}else if(rb5.Checked ){
				samplesPerSec = 44100;
			}
		
			if(rb6.Checked ){
				channel = 1;
			}else if(rb7.Checked ){
				channel = 2;
			}
		
		
		
			uint mci;
			string cmd;
		
			cmd = "open new Type waveaudio Alias capture";
			mci = mciSendString(cmd, (string) null, 0, 0);
			if(mci != 0){
				tb1.Text = "Open Error";
				return;
			}
		
			cmd = string.Format("set capture bitspersample {0:d}", bitPerSample);
			mci = mciSendString(cmd, (string) null, 0, 0);
			if(mci != 0){
				tb1.Text = "bitspersample set error";
				return;
			}
		
			cmd = string.Format("set capture samplespersec {0:d}", samplesPerSec);
			mci = mciSendString(cmd, (string) null, 0, 0);
			if(mci != 0){
				tb1.Text = "samplespersec set error";
				return;
			}
		
			cmd = string.Format("set capture channels {0:d}", channel);
			mci = mciSendString(cmd, (string) null, 0, 0);
			if(mci != 0){
				tb1.Text = "channels set error";
				return;
			}
		
			tb1.Text = "setting finished";
		}
		
		void Bt7Click(object sender, EventArgs e)
		{
			uint mci = mciSendString("record capture", (string) null, 0, 0);
			if(mci != 0){
				MessageBox.Show(mci.ToString());
				return;
			}
		
			tb1.Text = "start record";
		}
		
		void Bt8Click(object sender, EventArgs e)
		{
			uint mci = mciSendString("stop capture", (string) null, 0, 0);
			if(mci != 0) return;
		
			tb1.Text = "stop record";
		}
		
		void Bt9Click(object sender, EventArgs e)
		{
			string cmd;

			string szFilter = "음악파일|*.wav";
			SaveFileDialog sfd = new SaveFileDialog();
			sfd.DefaultExt = ".wav";
			sfd.OverwritePrompt = true;
			sfd.CreatePrompt = true;
			sfd.ValidateNames = true;
			sfd.Filter = szFilter;
			
			try {
				if(sfd.ShowDialog() == DialogResult.OK){
					cmd = "save capture \"" + sfd.FileName + "\"";
					uint mci = mciSendString(cmd, (string) null, 0, 0);
			
					if(mci != 0) return;
			
					tb1.Text = "saved";
				}
			} catch(System.Exception ex){
				MessageBox.Show(ex.Message);
			}
		}
		
		void Bt10Click(object sender, EventArgs e)
		{
			mciSendString("play capture from 0", (string) null, 0, 0);
		}
		
		void Bt11Click(object sender, EventArgs e)
		{
			uint mci = mciSendString("close capture", (string) null, 0, 0);
			if(mci != 0) return;
			tb1.Text = "closed capture";
		}
	}
}
